#include "dataclass.h"
#include <fstream>
#include <iostream>
#include <QDebug>

using namespace std;


//在主程序中需要做的事情：调用DataClass::findMaxMinData(int begin, int end)函数
//判断end-begin时候大于3941*2：
//若大于，maxData,minData
//若小于，maxData=minData


/* 通过调用DataClass::findMaxMinData(int begin, int end)函数；
 * 可以找到指定范围内数据的最大值和最小值数组，从而获得包络线
 * 而当begin与end之间的数据个数>3941*2的时候，进行取样处理
 * 而当begin与end之间的数据个数<3941*2的时候，将所有数据进行绘制
 *
 * ！！！这里的3940*4是可以更改的，在DataClass::findMaxMinData(int begin, int end)函数中的if语句中修改
*/


DataClass::DataClass(QObject *parent) : QObject(parent)
{

}


/*函数名称：txt2dataV
 * 函数功能：读取选取的二进制文件，然后把其中的数据存储到dataVector数组中
*/
void DataClass::txt2dataV(char *filePath){                   //这里的计算公式需要随时修改

    bool gpsCache[10];
    float canshu = 100.0;

    unsigned char data[16384];

    indexP=indexR=indexGPS=index0=index1=index2=index3=index4=index5=
            index6=index7=index8=index9=indexA=indexB=0;

    int flagPitch,flagRoll;

    // 以读模式打开文件
    ifstream infile;
    infile.open(filePath, ios::in | ios::binary);
    while (1) {
        if(!infile.read((char *)&data, sizeof(data))){
            break;
        }
        for (int i=0;i<8192;i++) {
            if (data[2*i+1]<16) {
                dataVector0[index0] = data[2*i+1]*256 + data[2*i];
                index0++;
            }else if (data[2*i+1]<32) {
                dataVector1[index1] = (data[2*i+1]-16)*256 + data[2*i];
                index1++;
            }else if (data[2*i+1]<48) {
                dataVector2[index2] = (data[2*i+1]-32)*256 + data[2*i];
                index2++;
            }else if (data[2*i+1]<64) {
                dataVector3[index3] = (data[2*i+1]-48)*256 + data[2*i];
                index3++;
            }else if (data[2*i+1]<80) {
                dataVector4[index4] = (data[2*i+1]-64)*256 + data[2*i];
                index4++;
            }else if (data[2*i+1]<96) {
                dataVector5[index5] = (data[2*i+1]-80)*256 + data[2*i];
                index5++;
            }
        }

        if(!infile.read((char *)&data, sizeof(data))){
            break;
        }
        for (int i=0;i<8192;i++) {
            if (data[2*i+1]<16) {
                dataVector6[index6] = data[2*i+1]*256 + data[2*i];
                index6++;
            }else if (data[2*i+1]<32) {
                dataVector7[index7] = (data[2*i+1]-16)*256 + data[2*i];
                index7++;
            }else if (data[2*i+1]<48) {
                dataVector8[index8] = (data[2*i+1]-32)*256 + data[2*i];
                index8++;
            }else if (data[2*i+1]<64) {
                dataVector9[index9] = (data[2*i+1]-48)*256 + data[2*i];
                index9++;
            }else if (data[2*i+1]<80) {
                dataVectorA[indexA] = (data[2*i+1]-64)*256 + data[2*i];
                indexA++;
            }else if (data[2*i+1]<96) {
                dataVectorB[indexB] = (data[2*i+1]-80)*256 + data[2*i];
                indexB++;
            }
        }
    }
    // 关闭打开的文件
    infile.close();
    //qDebug()<<"file over";


    /*改变采样率只影响串口信息的采集，和时间的确定。对其他通道的信息没有影响*/

    //下面对dataVectorB中的数据提取字节到gpsCache中
    int ininin = 1;//1000
    while(ininin<indexB){
        if((dataVectorB[ininin]<1000)&(dataVectorB[ininin+5]<1000)){//dataVectorB
            //读52个点，前50个点，每10个点取均值。舍掉后两个点
            for (int ii=0;ii<10;ii++) {
                int sumData=0;
                for (int inde=0;inde<10;inde++) {
                    sumData+=dataVectorB[ininin+inde];
                }
                if(sumData/10>1000){
                    gpsCache[ii] = 1;
                }else {
                    gpsCache[ii] = 0;
                }
                ininin+=10;
                if(ii==4|ii==9){
                    ininin+=2;
                }
            }
            gpsInformation[indexGPS] = gpsCache[1]+gpsCache[2]*2+gpsCache[3]*4+gpsCache[4]*8+
                    gpsCache[5]*16+gpsCache[6]*32+gpsCache[7]*64+gpsCache[8]*128;
            qDebug()<<gpsCache[1]<<gpsCache[2]<<gpsCache[3]<<gpsCache[4]
                    <<gpsCache[5]<<gpsCache[6]<<gpsCache[7]<<gpsCache[8];
            indexGPS++;
        }
        ininin++;
    }

    qDebug()<<"indexGPS="<<indexGPS;

    //下面将gpsCache中的字节进行挑选，并将数据存储到pitch和roll中
    for (int indInf = 0;indInf<indexGPS;indInf++) {
        qDebug()<<gpsInformation[indInf];
//        if(((gpsInformation[indInf+1]+gpsInformation[indInf+2]+gpsInformation[indInf+3]+
//                gpsInformation[indInf+4]+gpsInformation[indInf+5]+gpsInformation[indInf+6]+
//                gpsInformation[indInf+7]+gpsInformation[indInf+8]+gpsInformation[indInf+9]+
//                gpsInformation[indInf+10]+gpsInformation[indInf+11]+gpsInformation[indInf+12])%256==
//                gpsInformation[indInf+13])&(gpsInformation[indInf]==0x77)){
        if(gpsInformation[indInf]==119){
            //校验和检查完毕
            if(gpsInformation[indInf+4]/16){
                flagPitch = -1;
            }else {
                flagPitch = 1;
            }

            if(gpsInformation[indInf+7]/16){
                flagRoll = -1;
            }else {
                flagRoll = 1;
            }

            pitch[indexP] = ((gpsInformation[indInf+4]%16)*100+gpsInformation[indInf+5]/16*10+
                    gpsInformation[indInf+5]%16+
                    (gpsInformation[indInf+6]/16*10+gpsInformation[indInf+6]%16)/canshu)*flagPitch;
            //qDebug()<<"pitch="<<pitch[indexP];
            indexP++;

            roll[indexR] = ((gpsInformation[indInf+7]%16)*100+gpsInformation[indInf+8]/16*10+
                    gpsInformation[indInf+8]%16+
                    (gpsInformation[indInf+9]/16*10+gpsInformation[indInf+9]%16)/canshu)*flagRoll;
//            qDebug()<<"roll="<<roll[indexR];
//            qDebug()<<"119?="<<gpsInformation[indInf]<<"132?="<<gpsInformation[indInf+3];
//            qDebug()<<"pitch小数="<<gpsInformation[indInf+6]<<"roll小数="<<gpsInformation[indInf+9];
//            qDebug()<<"pitch整数="<<gpsInformation[indInf+5]<<"roll整数="<<gpsInformation[indInf+8];
//            qDebug()<<"sum="<<gpsInformation[indInf+1]+gpsInformation[indInf+2]+
//                    gpsInformation[indInf+3]+gpsInformation[indInf+4]+gpsInformation[indInf+5]+
//                    gpsInformation[indInf+6]+gpsInformation[indInf+7]+gpsInformation[indInf+8]+
//                    gpsInformation[indInf+9]+gpsInformation[indInf+10]+gpsInformation[indInf+11]+
//                    gpsInformation[indInf+12]<<"校验和="<<gpsInformation[indInf+13];
            qDebug()<<gpsInformation[indInf]<<gpsInformation[indInf+1]<<gpsInformation[indInf+2]<<
                    gpsInformation[indInf+3]<<gpsInformation[indInf+4]<<gpsInformation[indInf+5]<<
                    gpsInformation[indInf+6]<<gpsInformation[indInf+7]<<gpsInformation[indInf+8]<<
                    gpsInformation[indInf+9]<<gpsInformation[indInf+10]<<gpsInformation[indInf+11]<<
                    gpsInformation[indInf+12]<<gpsInformation[indInf+13];
            indexR++;
            indInf+=13;
        }
    }
}


/*函数名称：initializeMaxMin
 * 函数功能：初始化maxData和minData两个数组，清空所有数据
*/
void DataClass::initializeMaxMin(){//!!!!!这里的0需要修改
    //置零
    memset(indData, 0, 7882*sizeof(int));

    memset(maxData0, 0, 7882*sizeof(int));
    memset(maxData1, 0, 7882*sizeof(int));
    memset(maxData2, 0, 7882*sizeof(int));
    memset(maxData3, 0, 7882*sizeof(int));
    memset(maxData4, 0, 7882*sizeof(int));
    memset(maxData5, 0, 7882*sizeof(int));
    memset(maxData6, 0, 7882*sizeof(int));
    memset(maxData7, 0, 7882*sizeof(int));
    memset(maxData8, 0, 7882*sizeof(int));
    memset(maxData9, 0, 7882*sizeof(int));
    memset(maxDataA, 0, 7882*sizeof(int));

//    memset(minData0, 0, 7882*sizeof(int));
//    memset(minData1, 0, 7882*sizeof(int));
//    memset(minData2, 0, 7882*sizeof(int));
//    memset(minData3, 0, 7882*sizeof(int));
//    memset(minData4, 0, 7882*sizeof(int));
//    memset(minData5, 0, 7882*sizeof(int));
//    memset(minData6, 0, 7882*sizeof(int));
//    memset(minData7, 0, 7882*sizeof(int));
//    memset(minData8, 0, 7882*sizeof(int));
//    memset(minData9, 0, 7882*sizeof(int));
//    memset(minDataA, 0, 7882*sizeof(int));

    memset(pitchCa, 0, 1000*sizeof(float));
    memset(rollCa, 0, 1000*sizeof(float));

}

/*函数名称：findMax
 * 参数1：*dataVector：进行操作的数组
 * 参数2：beginN：开始位置
 * 参数3：sizeMe：数据长度
 * 函数功能：找到并返回dataVector数组中，beginN开始的，长度为sizeMe的数据中的最大值、最小值，
 * 并存储到全局变量max、min中
*/
void DataClass::findMaxMin(int *dataVector,int beginN, int sizeMe){
    max = dataVector[beginN];
    min = dataVector[beginN];
    for (int i=beginN; i<beginN+sizeMe; i++) {
        if(dataVector[i]>max){
            max = dataVector[i];
        }
        if(dataVector[i]<min){
            min = dataVector[i];
        }
    }
}



/*函数名称：findMaxMinData
 * 参数1：begin：data数组中数据开始角标
 * 参数2：end：data数组中数据结束角标
 * 函数功能：处理data数组中begin到end的数据，进行分割，找到其中各个小组的最大值和最小值，
 *          并存储在maxData和minData数组中
*/
void DataClass::findMaxMinData(int begin, int end, bool *flag){

    initializeMaxMin();

    int number = end-begin;
    int remain;

    if(flag[11]){
        for (int i=0;i<indexP;i++) {
            pitchCa[i] = pitch[i];
        }
    }else{
        for (int i=0;i<indexP;i++) {
            pitchCa[i] = 0;
        }
    }
    if(flag[12]){
        for (int i=0;i<indexP;i++) {
            rollCa[i] = roll[i];
        }
    }else{
        for (int i=0;i<indexP;i++) {
            rollCa[i] = 0;
        }
    }

    if(number>7882){
        //采点
        int size = number/3940;
        for (int i = 0; i<3940;i++) {
            if(flag[0]){
                findMaxMin(dataVector0,i*size+begin, size);
                maxData0[i] = max;
                //minData0[i] = min;
            }else{
                //maxData0[i] = minData0[i] = 0;
                maxData0[i] = 0;
            }

            if(flag[1]){
                findMaxMin(dataVector1,i*size+begin, size);
                maxData1[i] = max;
                //minData1[i] = min;
            }else{
                //maxData1[i] = minData1[i] = 0;
                maxData1[i] = 0;
            }

            if(flag[2]){
                findMaxMin(dataVector2,i*size+begin, size);
                maxData2[i] = max;
                //minData2[i] = min;
            }else{
                //maxData2[i] = minData2[i] = 0;
                maxData2[i] = 0;
            }

            if(flag[3]){
                findMaxMin(dataVector3,i*size+begin, size);
                maxData3[i] = max;
                //minData3[i] = min;
            }else{
                //maxData3[i] = minData3[i] = 0;
                maxData3[i] = 0;
            }

            if(flag[4]){
                findMaxMin(dataVector4,i*size+begin, size);
                maxData4[i] = max;
                //minData4[i] = min;
            }else{
                //maxData4[i] = minData4[i] = 0;
                maxData4[i] = 0;
            }

            if(flag[5]){
                findMaxMin(dataVector5,i*size+begin, size);
                maxData5[i] = max;
                //minData5[i] = min;
            }else{
                //maxData5[i] = minData5[i] = 0;
                maxData5[i] = 0;
            }

            if(flag[6]){
                findMaxMin(dataVector6,i*size+begin, size);
                maxData6[i] = max;
                //minData6[i] = min;
            }else{
                //maxData6[i] = minData6[i] = 0;
                maxData6[i] = 0;
            }

            if(flag[7]){
                findMaxMin(dataVector7,i*size+begin, size);
                maxData7[i] = max;
                //minData7[i] = min;
            }else{
                //maxData7[i] = minData7[i] = 0;
                maxData7[i] = 0;
            }

            if(flag[8]){
                findMaxMin(dataVector8,i*size+begin, size);
                maxData8[i] = max;
                //minData8[i] = min;
            }else{
                //maxData8[i] = minData8[i] = 0;
                maxData8[i] = 0;
            }

            if(flag[9]){
                findMaxMin(dataVector9,i*size+begin, size);
                maxData9[i] = max;
                //minData9[i] = min;
            }else{
                //maxData9[i] = minData9[i] = 0;
                maxData9[i] = 0;
            }

            if(flag[10]){
                findMaxMin(dataVectorA,i*size+begin, size);
                maxDataA[i] = max;
                //minDataA[i] = min;
            }else{
                //maxDataA[i] = minDataA[i] = 0;
                maxDataA[i] = 0;
            }

            indData[i] = i*size+begin;
        }
        if(number>size*3940){
            remain = number-size*3940;

            if(flag[0]){
                findMaxMin(dataVector0,3940*size+begin, remain);
                maxData0[3940] = max;
                //minData0[3940] = min;
            }else{
                //maxData0[3940] = minData0[3940] = 0;
                maxData0[3940] = 0;
            }

            if(flag[1]){
                findMaxMin(dataVector1,3940*size+begin, remain);
                maxData1[3940] = max;
                //minData1[3940] = min;
            }else{
                //maxData1[3940] = minData1[3940] = 0;
                maxData1[3940] = 0;
            }

            if(flag[2]){
                findMaxMin(dataVector2,3940*size+begin, remain);
                maxData2[3940] = max;
                //minData2[3940] = min;
            }else{
                //maxData2[3940] = minData2[3940] = 0;
                maxData2[3940] = 0;
            }

            if(flag[3]){
                findMaxMin(dataVector3,3940*size+begin, remain);
                maxData3[3940] = max;
                //minData3[3940] = min;
            }else{
                //maxData3[3940] = minData3[3940] = 0;
                maxData3[3940] = 0;
            }

            if(flag[4]){
                findMaxMin(dataVector4,3940*size+begin, remain);
                maxData4[3940] = max;
                //minData4[3940] = min;
            }else{
                //maxData4[3940] = minData4[3940] = 0;
                maxData4[3940] = 0;
            }

            if(flag[5]){
                findMaxMin(dataVector5,3940*size+begin, remain);
                maxData5[3940] = max;
                //minData5[3940] = min;
            }else{
                //maxData5[3940] = minData5[3940] = 0;
                maxData5[3940] = 0;
            }

            if(flag[6]){
                findMaxMin(dataVector6,3940*size+begin, remain);
                maxData6[3940] = max;
                //minData6[3940] = min;
            }else{
                //maxData6[3940] = minData6[3940] = 0;
                maxData6[3940] = 0;
            }

            if(flag[7]){
                findMaxMin(dataVector7,3940*size+begin, remain);
                maxData7[3940] = max;
                //minData7[3940] = min;
            }else{
                //maxData7[3940] = minData7[3940] = 0;
                maxData7[3940] = 0;
            }

            if(flag[8]){
                findMaxMin(dataVector8,3940*size+begin, remain);
                maxData8[3940] = max;
                //minData8[3940] = min;
            }else{
                //maxData8[3940] = minData8[3940] = 0;
                maxData8[3940] = 0;
            }

            if(flag[9]){
                findMaxMin(dataVector9,3940*size+begin, remain);
                maxData9[3940] = max;
                //minData9[3940] = min;
            }else{
                //maxData9[3940] = minData9[3940] = 0;
                maxData9[3940] = 0;
            }

            if(flag[10]){
                findMaxMin(dataVectorA,3940*size+begin, remain);
                maxDataA[3940] = max;
                //minDataA[3940] = min;
            }else{
                //maxDataA[3940] = minDataA[3940] = 0;
                maxDataA[3940] = 0;
            }

            indData[3940] = 3940*size+begin;
        }
    }else{
        //直接画
        for (int in=0;in<number;in++) {
            if(flag[0]){
                maxData0[in] = dataVector0[in+begin];
                //minData0[in] = dataVector0[in+begin];
            }else{
                //maxData0[in] = minData0[in] = 0;
                maxData0[in] = 0;
            }

            if(flag[1]){
                maxData1[in] = dataVector1[in+begin];
                //minData1[in] = dataVector1[in+begin];
            }else{
                //maxData1[in] = minData1[in] = 0;
                maxData1[in] = 0;
            }

            if(flag[2]){
                maxData2[in] = dataVector2[in+begin];
                //minData2[in] = dataVector2[in+begin];
            }else{
                //maxData2[in] = minData2[in] = 0;
                maxData2[in] = 0;
            }

            if(flag[3]){
                maxData3[in] = dataVector3[in+begin];
                //minData3[in] = dataVector3[in+begin];
            }else{
                //maxData3[in] = minData3[in] = 0;
                maxData3[in] = 0;
            }

            if(flag[4]){
                maxData4[in] = dataVector4[in+begin];
                //minData4[in] = dataVector4[in+begin];
            }else{
                //maxData4[in] = minData4[in] = 0;
                maxData4[in] = 0;
            }

            if(flag[5]){
                maxData5[in] = dataVector5[in+begin];
                //minData5[in] = dataVector5[in+begin];
            }else{
                //maxData5[in] = minData5[in] = 0;
                maxData5[in] = 0;
            }

            if(flag[6]){
                maxData6[in] = dataVector6[in+begin];
                //minData6[in] = dataVector6[in+begin];
            }else{
                //maxData6[in] = minData6[in] = 0;
                maxData6[in] = 0;
            }

            if(flag[7]){
                maxData7[in] = dataVector7[in+begin];
                //minData7[in] = dataVector7[in+begin];
            }else{
                //maxData7[in] = minData7[in] = 0;
                maxData7[in] = 0;
            }

            if(flag[8]){
                maxData8[in] = dataVector8[in+begin];
                //minData8[in] = dataVector8[in+begin];
            }else{
                //maxData8[in] = minData8[in] = 0;
                maxData8[in] = 0;
            }

            if(flag[9]){
                maxData9[in] = dataVector9[in+begin];
                //minData9[in] = dataVector9[in+begin];
            }else{
                //maxData9[in] = minData9[in] = 0;
                maxData9[in] = 0;
            }

            if(flag[10]){
                maxDataA[in] = dataVectorA[in+begin];
                //minDataA[in] = dataVectorA[in+begin];
            }else{
                //maxDataA[in] = minDataA[in] = 0;
                maxDataA[in] = 0;
            }

            indData[in] = in+begin;
        }
    }

    //有两种情况
    //第一种：在调用该方法的程序中判断
    //第二种：在该方法中判断,主程序中判断之后，只画maxData即可
    //分析：如果采用第一种方法，会造成主程序逻辑复杂，个人感觉第二种方法更好
}
